package cn.deschen.app.controller;

import cn.deschen.app.vo.RegionDetailVO;
import cn.deschen.biz.domain.entity.AdRegion;
import cn.deschen.biz.dto.region.RegionAddDTO;
import cn.deschen.biz.dto.region.RegionQueryDTO;
import cn.deschen.biz.dto.region.RegionUpdateDTO;
import cn.deschen.biz.service.AdRegionService;
import cn.deschen.common.api.BizResCode;
import cn.deschen.common.api.CmPage;
import cn.deschen.common.api.CmRes;
import cn.deschen.common.api.CmResCode;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Author hanbin_chen
 * @Description 地区控制层
 * @DateTime 2021/06/20
 * @Version V1.0.0
 */
@RestController
@Slf4j
@RequestMapping("/ad/region")
@Api(tags = "地区管理")
public class AdRegionController {

    @Autowired
    private AdRegionService regionService;

    @Autowired
    private MapperFacade mapperFacade;

    @ApiOperation("创建地区")
    @PostMapping()
    public CmRes<Void> add(@Valid @RequestBody RegionAddDTO addDTO) {
        boolean ifExisted = checkIfExisted(addDTO.getProvince(), addDTO.getCity());
        if (ifExisted) {
            return CmRes.failure(CmResCode.REPEAT_OPERATION.extra("该地区已存在"));
        }
        AdRegion region = mapperFacade.map(addDTO, AdRegion.class);
        regionService.save(region);
        return CmRes.success();
    }

    private boolean checkIfExisted(String province, String city) {
        QueryWrapper<AdRegion> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(AdRegion::getProvince, province)
                .eq(AdRegion::getCity, city);
        AdRegion region = regionService.getOne(wrapper);
        if (null != region) {
            return true;
        }
        return false;
    }

    @ApiOperation("更新地区")
    @PutMapping()
    public CmRes<Void> update(@Valid @RequestBody RegionUpdateDTO updateDTO) {
        AdRegion region = regionService.getById(updateDTO.getId());
        if (null == region) {
            return CmRes.failure(CmResCode.DATA_NOT_FOUND);
        }
        if (!region.getProvince().equals(updateDTO.getProvince())
                || !region.getCity().equals(updateDTO.getCity())) {
            boolean ifExisted = checkIfExisted(updateDTO.getProvince(), updateDTO.getCity());
            if (ifExisted) {
                return CmRes.failure(BizResCode.REGION_IS_REGISTERED);
            }
        }

        AdRegion newRegion = mapperFacade.map(updateDTO, AdRegion.class);
        regionService.updateById(newRegion);
        return CmRes.success();
    }

    @ApiOperation("删除地区")
    @DeleteMapping()
    public CmRes<Void> delete(@RequestParam List<Long> ids) {
        regionService.removeByIds(ids);
        return CmRes.success();
    }

    @ApiOperation("获取地区列表")
    @GetMapping("/page")
    public CmRes<CmPage<AdRegion>> page(RegionQueryDTO queryDTO,
                                        @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                        @RequestParam(value = "size", required = false, defaultValue = "10") Integer size) {
        PageHelper.startPage(page, size);
        QueryWrapper<AdRegion> wrapper = new QueryWrapper<>();
        wrapper.lambda().orderByDesc(AdRegion::getUpdateTime);
        if (!StringUtils.isEmpty(queryDTO.getProvince())) {
            wrapper.lambda().like(AdRegion::getProvince, queryDTO.getProvince());
        }
        if (!StringUtils.isEmpty(queryDTO.getCity())) {
            wrapper.lambda().like(AdRegion::getCity, queryDTO.getCity());
        }
        List<AdRegion> regionList = regionService.list(wrapper);
        return CmRes.success(CmPage.restPage(regionList));
    }

    @ApiOperation("根据主键获取地区详情")
    @GetMapping("/detail")
    public CmRes<RegionDetailVO> detail(@ApiParam("地区id") @RequestParam Long id) {

        AdRegion region = regionService.getById(id);
        RegionDetailVO detailVO = mapperFacade.map(region, RegionDetailVO.class);
        return CmRes.success(detailVO);
    }
}


